//
//  ResponderViewController.m
//  MyProject
//
//  Created by yun on 2018/5/7.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "ResponderViewController.h"
#import "MyButton.h"

@interface ResponderViewController ()

@property (nonatomic,strong) UIView *baseView;

@end

@implementation ResponderViewController
/*
 发生触摸事件后，系统会将该事件加入到一个由UIApplication管理的事件队列中,为什么是队列而不是栈？因为队列的特点是FIFO，即先进先出，先产生的事件先处理才符合常理，所以把事件添加到队列。
 UIApplication会从事件队列中取出最前面的事件，并将事件分发下去以便处理，通常，先发送事件给应用程序的主窗口（keyWindow）。
 主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件，这也是整个事件处理过程的第一步。
 找到合适的视图控件后，就会调用视图控件的touches方法来作具体的事件处理。
 
 事件传递：
 UIApplication( queue[event1,event2,...] ) ---event1---> keyWindow ---event1---> VC/View
 事件处理：
 反着来
 
 注：如果父控件不能接受触摸事件，那么子控件就不可能接收到触摸事件
 
 UIView不能接收触摸事件的三种情况：
 不允许交互：userInteractionEnabled = NO
 隐藏：如果把父控件隐藏，那么子控件也会隐藏，隐藏的控件不能接受事件
 透明度：如果设置一个控件的透明度<0.01，会直接影响子控件的透明度。alpha：0.0~0.01为透明。
 
 注 意:默认UIImageView不能接受触摸事件，因为不允许交互，即userInteractionEnabled = NO。所以如果希望UIImageView可以交互，需要设置UIImageView的userInteractionEnabled = YES。
 
 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;返回的View才是真正处理事件的View
 
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.baseView = [[UIView alloc] init];
    self.baseView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.baseView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    [self gesture];
}

- (void)gesture{
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.baseView addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)pan{
    CGPoint p1 = [pan locationInView:pan.view];
    CGPoint p2 = [pan translationInView:pan.view];
    CGPoint p3 = [pan velocityInView:pan.view];
    NSLog(@"p1(%.2f,%.2f)===p2(%.2f,%.2f)===p3(%.2f,%.2f)===",p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
}

- (void)event1{
    UIView *ba = [[UIView alloc] init];
    
    ba.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:ba];
    [ba mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    MyButton *sender = [MyButton new];
    sender.backgroundColor = [UIColor redColor];
    [sender addTarget:self action:@selector(senderClick:) forControlEvents:UIControlEventTouchUpInside];
    [ba addSubview:sender];
    [sender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(ba);
        make.left.equalTo(ba.mas_right).offset(-50);
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
}


- (void)senderClick:(MyButton *)b{
//    NSLog(@"点击");
    [b back];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
